<?php
namespace app\admin\controller;
use app\common\controller\AdminController;
use app\common\model\GzhTitle as  model;
use think\facade\View;


class GzhTitle extends AdminController {

    public function index(){

        if($this->request->isAjax()){
            $where_caogao = [];
            $where_pub = [];
            $where_article = [];
            $where_title = [];
            $title           = $this->request->param('title','');
            $is_caogao         = $this->request->param('is_caogao',0);
            $is_pub         = $this->request->param('is_pub',0);
            $is_article         = $this->request->param('is_article',0);
            if($title!=''){$where_title = '( title like "%'.$title.'%")';}
            if($is_caogao>0){
                $where_caogao = ['is_caogao'=>$is_caogao];
            }

            if($is_article>0){
                $where_article = ['is_article'=>$is_article];
            }

            if($is_pub>0){
                $where_pub = ['is_pub'=>$is_pub];
            }

            $model = new model();
            $re['code'] = 0;
            $re['msg'] = '';
            $re['data'] = [];
            $re['count'] = 0;
            $res = $model->order('id','desc')
                ->where($where_article)
                ->where($where_caogao)
                ->where($where_pub)
                ->where($where_title)
                ->with(['gzhName'])
                ->withAttr('public_at',function ($value,$data){
                    return date('Y-m-d H:i:s',$data['pub_time']);
                })
                ->append(['public_at'])
                ->paginate(['list_rows'=>$this->limit,'page'=>$this->page])
                ->toArray();
            if(sizeof($res['data'])>0){
                $re['data'] = $res['data'];
                $re['count'] = $res['total'];
            }
            return $re;
        }else{
            return View::fetch();

        }
    }


    public function publog(){

        if($this->request->isAjax()){
            $where_time = [];
            $where_title = [];
            $where_appid = [];
            $title           = $this->request->param('title','');
            $appid           = $this->request->param('appid','');
            $post            = $this->request->param();
            if($title!=''){$where_title = '( title like "%'.$title.'%")';}
            if($appid!=''){$where_appid = ['appid'=>$appid];}

            if (isset($post['date']) && $post['date'] != "") {
                $date    = explode(' 到 ', $post['date']);
                $date1 = strtotime($date[0] . ' 00:00:00');
                $date2 = strtotime($date[1] . ' 23:59:59');
                $where_time = "(pub_time between ".$date1." and ".$date2.")";
            }


            $model = new model();
            $re['code'] = 0;
            $re['msg'] = '';
            $re['data'] = [];
            $re['count'] = 0;
            $res = $model->order('pub_time','desc')
                ->where('pub_time','>',0)
                ->where($where_title)
                ->where($where_time)
                ->where($where_appid)
                ->with(['gzhName'])
                ->withAttr('public_at',function ($value,$data){
                    return date('Y-m-d H:i:s',$data['pub_time']);

                })
                ->append(['public_at'])
                ->paginate(['list_rows'=>$this->limit,'page'=>$this->page])
                ->toArray();
            if(sizeof($res['data'])>0){
                $re['data'] = $res['data'];
                $re['count'] = $res['total'];
            }
            return $re;
        }else{
            $accountModel = new \app\common\model\GzhAccount();
            $account_list = $accountModel->where('is_use',2)->order('id','desc')->select()->toArray();
            View::assign('account_list',$account_list);
            return View::fetch();

        }
    }

    public function edit()
    {
        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        View::engine()->layout(false);
        $model = new model();
        if($this->request->isPost())
        {
            $data = $this->request->param();
            if($this->request->param('id')){
                unset($data['id']);
                $model->strict(false)->where('id',$this->request->param('id'))->save($data);
            }else{
                $model->strict(false)->insert($data);
            }

            $this->setActionLog('编辑标题',$this->admin_info['name'].'：编辑标题'.$this->request->param('id'));
            $status = [
                'status' => true,
                'msg' => '修改成功',
                'data' => []
            ];
            return  $status;
        }

        if($this->request->param('id'))
        {
            //编辑页面
            $info = $model->where(['id'=>$this->request->param('id')])->find();
            if(!$info)
            {
                return error_code(10000);
            }
            View::assign('data',$info);
        }
        $result['status'] = true;
        $result['msg'] = '修改成功';
        $result['data'] = View::fetch();
        return $result;
    }


    public function add(){

        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        View::engine()->layout(false);
        $model = new model();

        if($this->request->isPost())
        {
            $data = $this->request->param();
            $data['created_at'] = TIMESTAMP;
            if($data['titles']!=''){
                $content = $data['titles'];
                $titles = explode("\n", $content);
                $titles = array_filter($titles, function($title) {
                    return !empty(trim($title));
                });
                $datatitls = [];
                foreach ($titles as $title) {
                    $datatitls[] = [
                        'title' => trim($title),
                        'long_title' => '《'.trim($title).'》'.$data['keyword'],
                        'keyword'=>$data['keyword'],
                        'created_at' => TIMESTAMP,
                    ];
                }
                $model->strict(false)->insertAll($datatitls);
                $count = sizeof($datatitls);
                $this->setActionLog('添加标题',$this->admin_info['name'].'：批量添加了'.$count.'个标题');
            }else{
                $id = $model->strict(false)->insertGetId($data);
                $this->setActionLog('添加标题',$this->admin_info['name'].'：添加标题'.$id);
            }

            $status = [
                'status' => true,
                'msg' => '添加成功',
                'data' => []
            ];
            return  $status;
        }
        $result['status'] = true;
        $result['msg'] = '成功';
        $result['data'] = View::fetch();
        return $result;
    }


    public function del(){
        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        $id = $this->request->param('id');
        $model = new model();
        $res = $model->where('id',$id)->delete();
        if($res) {
            $this->setActionLog('删除标题',$this->admin_info['name'].'：删除标题'.$id);
            $result['status'] = true;
            $result['msg'] = '删除成功';
        }else{
            $result['msg'] = '删除失败，请联系管理员';
        }
        return $result;
    }

}
